package com.sxm.yiwei.pinche;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.beans.pinche.HostOrder;
import com.sxm.yiwei.beans.pinche.PassengerOrder;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.utils.DateUtil;
import com.sxm.yiwei.utils.HandleDataUtil;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class OrderConfirmActivity extends BaseActivity implements View.OnClickListener{

    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_style;
    private TextView tv_distance;
    private TextView tv_start_pos;
    private TextView tv_end_pos;
    private TextView tv_start_time;
    private TextView tv_order_id;
    private TextView tv_pinche_count_rule;
    private TextView tv_pinche_rule;
    private EditText et_description;
    private CheckBox checkBox;
    private String serviceId;
    private int identity;
    private int type;
    private Button btn_release_now;
    private TextView tv_seats_need;
    private TextView tv_seats_support;
    private TextView tv_price;
    private TextView tv_total_price;
    private TextView tv_break_appointment_money;
    private Button btn_exemption_shuangyue;
    private TextView tv_rest_count;
    private TextView tv_space;
    private TextView tv_transfer_fee;
    private TextView tv_baojia;
    private TextView tv_transfer_baojia;
    private ProgressDialog progressDialog;
    private String departTime;

    @Override
    public void setContentView() {

        serviceId=getIntent().getStringExtra("serviceId");
        identity = getIntent().getIntExtra("identity",-1);
        type = getIntent().getIntExtra("type",-1);
        departTime=getIntent().getStringExtra("departTime");
        if (identity==Constant.PASSENGER){
            if (type==Constant.LONG_HURRY){
                setContentView(R.layout.activity_order_confirm_passenger_long_hurry);
            }else {
                setContentView(R.layout.activity_order_confirm_passenger_long);
            }
        }else if (identity==Constant.HOST){
            if (type==Constant.LONG_HURRY){
                setContentView(R.layout.activity_order_confirm_host_long_hurry);
            }else {
                setContentView(R.layout.activity_order_confirm_host_long);
            }
        }

    }

    @Override
    public void initData() {
        tv_title.setText("订单确认");
        if (identity==Constant.PASSENGER){
            if (type==Constant.LONG_HURRY){
                showToast("乘客长途急件");
            }else {
                JSONObject object = new JSONObject();
                object.put("serviceId",serviceId);
                httpGet(Common.HOST+Common.PASSENGER_PUBLISH_EXACTLY,object,Constant.PASSENGER_PUBLISH_EXACTLY);
            }
        }else if (identity==Constant.HOST){
            if (type==Constant.LONG_HURRY){
                showToast("车主长途急件");

            }else {
                JSONObject object = new JSONObject();
                object.put("serviceId",serviceId);
                httpGet(Common.HOST+Common.HOST_PUBLISH_EXACTLY,object,Constant.HOST_PUBLISH_EXACTLY);
            }
        }



    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_style = (TextView)findViewById(R.id.tv_style);
        tv_distance = (TextView)findViewById(R.id.tv_distance);
        tv_start_pos = (TextView)findViewById(R.id.tv_start_pos);
        tv_end_pos = (TextView)findViewById(R.id.tv_end_pos);
        tv_start_time = (TextView)findViewById(R.id.tv_start_time);
        tv_order_id = (TextView)findViewById(R.id.tv_order_id);
        tv_pinche_count_rule = (TextView)findViewById(R.id.tv_pinche_count_rule);
        tv_pinche_rule = (TextView)findViewById(R.id.tv_pinche_rule);
        et_description = (EditText)findViewById(R.id.et_description);
        checkBox = (CheckBox)findViewById(R.id.checkbox);
        btn_release_now=(Button)findViewById(R.id.btn_release_now);

        if (identity==Constant.PASSENGER){
            if (type==Constant.LONG_HURRY){
                tv_space = (TextView)findViewById(R.id.text3);
                tv_transfer_fee = (TextView)findViewById(R.id.text11);
                tv_baojia = (TextView)findViewById(R.id.text21);
                tv_transfer_baojia = (TextView)findViewById(R.id.tv_transfer_baojia);
            }else {
                tv_seats_need = (TextView)findViewById(R.id.tv_seats_need);
                tv_total_price = (TextView)findViewById(R.id.tv_total_price);
            }
        }else if (identity==Constant.HOST){
            if (type==Constant.LONG_HURRY){
                tv_space = (TextView)findViewById(R.id.text3);
                tv_transfer_fee = (TextView)findViewById(R.id.text11);
                tv_break_appointment_money=(TextView)findViewById(R.id.tv_loss_money);
            }else {
                tv_seats_support = (TextView)findViewById(R.id.tv_seats_support);
                tv_price = (TextView)findViewById(R.id.tv_price);
                tv_total_price = (TextView)findViewById(R.id.tv_total_price);
                tv_break_appointment_money = (TextView)findViewById(R.id.tv_loss_money);
                btn_exemption_shuangyue = (Button)findViewById(R.id.btn_exemption_shuangyue);
                tv_rest_count = (TextView)findViewById(R.id.tv_rest_count);
            }
        }

    }

    @Override
    public void initListener() {
        rl_back.setOnClickListener(this);
        tv_pinche_rule.setOnClickListener(this);
        tv_pinche_count_rule.setOnClickListener(this);
        btn_release_now.setOnClickListener(this);
        if (identity==Constant.HOST){
            if (type!=Constant.LONG_HURRY){
                btn_exemption_shuangyue.setOnClickListener(this);
            }
        }

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    btn_release_now.setEnabled(true);
                    btn_release_now.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_enable));
                    if (tv_rest_count!=null){
                        if (Integer.parseInt(HandleDataUtil.withoutChinese(tv_rest_count.getText().toString().trim()))!=0){
                            btn_exemption_shuangyue.setEnabled(true);
                            btn_exemption_shuangyue.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_enable));
                        }else {
                            btn_exemption_shuangyue.setEnabled(false);
                            btn_exemption_shuangyue.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_disable));
                        }
                    }


                }else {
                    btn_release_now.setEnabled(false);
                    btn_release_now.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_disable));
                    btn_exemption_shuangyue.setEnabled(false);
                    btn_exemption_shuangyue.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_disable));
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_pinche_count_rule:
                Intent countIntent = new Intent(this, RuleActivity.class);
                countIntent.putExtra("type", Constant.PINCHE_COUNT_RULE);
                startActivity(countIntent);
                break;
            case R.id.tv_pinche_rule:
                Intent countIntent1 = new Intent(this, RuleActivity.class);
                countIntent1.putExtra("type", Constant.PINCHE_COUNT_RULE);
                startActivity(countIntent1);
                break;
            case R.id.btn_release_now:
                if (identity==Constant.PASSENGER){
                    if (type==Constant.LONG_HURRY){

                    }else {

                        JSONObject object = new JSONObject();
                        object.put("serviceId",serviceId);
                        object.put("pinServiceAccount", HandleDataUtil.withoutChinese(tv_total_price.getText().toString().trim()));
                        object.put("pinLeaveWords",et_description.getText().toString());
                        httpPost(Common.HOST+Common.PASSENGER_PUBLISH_NOW,object,Constant.PASSENGER_PUBLISH_NOW);
                    }
                }else if (identity==Constant.HOST){
                    if (type==Constant.LONG_HURRY){

                    }else {
                        JSONObject object = new JSONObject();
                        object.put("serviceId",serviceId);
                        object.put("pinServiceAccount", HandleDataUtil.withoutChinese(tv_price.getText().toString().trim()));//单价
                        object.put("pinTotalAccount",HandleDataUtil.withoutChinese(tv_total_price.getText().toString().trim()));//总价
                        object.put("pinLoseAccount",HandleDataUtil.withoutChinese(tv_break_appointment_money.getText().toString().trim()));//爽约金
                        object.put("pinFreeFlag","0");//0：需支付爽约金；1：免爽约金
                        object.put("pinLeaveWords",et_description.getText().toString());//可为空
                        Log.e("免缴爽约金","serviceId="+serviceId+" pinServiceAccount="+HandleDataUtil.withoutChinese(tv_price.getText().toString().trim())+" pinTotalAccount"+HandleDataUtil.withoutChinese(tv_total_price.getText().toString().trim())+" pinLoseAccount="+HandleDataUtil.withoutChinese(tv_break_appointment_money.getText().toString().trim()));
                        httpPost(Common.HOST+Common.HOST_PUBLISH_NOW,object,Constant.HOST_PUBLISH_NOW_NEED_LOSS);
                    }
                }
                break;
            case R.id.btn_exemption_shuangyue:
                showToast("免缴爽约金");
                if (Long.parseLong(departTime)<= SystemClock.currentThreadTimeMillis()){
                    Toast.makeText(this,"当前时间超过出发时间，请重新发布",Toast.LENGTH_SHORT).show();
                    return;
                }
                noLossFormOrder();
                break;
        }
    }

    private void noLossFormOrder() {
        JSONObject object = new JSONObject();
        object.put("serviceId",serviceId);
        object.put("pinServiceAccount", HandleDataUtil.withoutChinese(tv_price.getText().toString().trim()));//单价
        object.put("pinTotalAccount",HandleDataUtil.withoutChinese(tv_total_price.getText().toString().trim()));//总价
        object.put("pinLoseAccount",HandleDataUtil.withoutChinese(tv_break_appointment_money.getText().toString().trim()));//爽约金
        object.put("pinFreeFlag","1");//0：需支付爽约金；1：免爽约金
        object.put("pinLeaveWords",et_description.getText().toString());//可为空
        httpPost(Common.HOST+Common.HOST_PUBLISH_NOW,object, Constant.HOST_PUBLISH_NOW_NOT_NEED_LOSS);
    }

    @Override
    public void onSuccess(String result, String fromWhere) {
        super.onSuccess(result,fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        if (fromWhere.equals(Constant.PASSENGER_PUBLISH_EXACTLY)){
            JSONObject data = obj.getJSONObject("data");
            PassengerOrder passengerOrder = new PassengerOrder();
            passengerOrder.setDepart_time(DateUtil.times(data.getLong("leaveOffTime")));
            passengerOrder.setSeats_need(data.getIntValue("passerRequireSeats"));
            passengerOrder.setEnd_place(data.getString("pinEndPoint"));
            passengerOrder.setPrice(data.getDouble("pinServiceAccount"));
            passengerOrder.setService_code(data.getString("pinServiceCode"));
            passengerOrder.setStyle(data.getIntValue("pinServiceKinds"));
            passengerOrder.setStart_place(data.getString("pinStartPoint"));
            passengerOrder.setDistance(data.getDouble("pinTotalDistance"));
            passengerOrder.setService_id(data.getString("serviceId"));
            passengerOrder.setTotal_price(passengerOrder.getPrice()*passengerOrder.getSeats_need());

            tv_seats_need.setText(passengerOrder.getSeats_need()+"个");
            tv_total_price.setText(passengerOrder.getTotal_price()+"元");
            if (passengerOrder.getStyle()==1){//长途
                tv_style.setText("长途");
            }else if (passengerOrder.getStyle()==2){//旅游
                tv_style.setText("旅游");
            }else if (passengerOrder.getStyle()==3){//同城
                tv_style.setText("同城");
            }else if (passengerOrder.getStyle()==0){//默认？
                tv_style.setText("长途");
            }
            tv_distance.setText(passengerOrder.getDistance()+"公里");
            tv_start_pos.setText(passengerOrder.getStart_place());
            tv_end_pos.setText(passengerOrder.getEnd_place());
            tv_start_time.setText(passengerOrder.getDepart_time());
            tv_order_id.setText(passengerOrder.getService_code());
        }else if (fromWhere.equals(Constant.HOST_PUBLISH_EXACTLY)){
            JSONObject data = obj.getJSONObject("data");
            HostOrder hostOrder = new HostOrder();
            hostOrder.setDepart_time(DateUtil.times(data.getLong("leaveOffTime")));
            hostOrder.setSeats(data.getIntValue("ownerHasSeats"));
            hostOrder.setEnd_place(data.getString("pinEndPoint"));

            hostOrder.setBreak_money(data.getDouble("pinLoseAccount"));
            hostOrder.setRest_count(data.getIntValue("pinLoseCount"));//免缴爽约金次数
            hostOrder.setTotal_price(data.getDouble("pinTotalAccount"));

            hostOrder.setPrice(data.getDouble("pinServiceAccount"));
            hostOrder.setService_code(data.getString("pinServiceCode"));
            hostOrder.setStyle(data.getIntValue("pinServiceKinds"));
            hostOrder.setStart_place(data.getString("pinStartPoint"));
            hostOrder.setDistance(data.getDouble("pinTotalDistance"));
            hostOrder.setService_id(data.getString("serviceId"));

            if (hostOrder.getStyle()==1){//长途
                tv_style.setText("长途");
            }else if (hostOrder.getStyle()==2){//旅游
                tv_style.setText("旅游");
            }else if (hostOrder.getStyle()==3){//同城
                tv_style.setText("同城");
            }else if (hostOrder.getStyle()==0){//默认？
                tv_style.setText("");
            }
            tv_distance.setText(hostOrder.getDistance()+"公里");
            tv_start_pos.setText(hostOrder.getStart_place());
            tv_end_pos.setText(hostOrder.getEnd_place());
            tv_start_time.setText(hostOrder.getDepart_time());
            tv_order_id.setText(hostOrder.getService_code());

            tv_seats_support.setText(hostOrder.getSeats()+"个");
            tv_total_price.setText(hostOrder.getTotal_price()+"元");
            tv_price.setText(hostOrder.getPrice()+"元");
            tv_break_appointment_money.setText(hostOrder.getBreak_money()+"元");
            tv_rest_count.setText("剩余"+hostOrder.getRest_count()+"次机会");
        }else if (fromWhere.equals(Constant.PASSENGER_PUBLISH_NOW)){
            JSONObject data = obj.getJSONObject("data");
            Intent intent = new Intent(OrderConfirmActivity.this, BookPayActivity.class);
            intent.putExtra("orderCode",data.getString("orderId"));
            intent.putExtra("amount",HandleDataUtil.withoutChinese(tv_total_price.getText().toString().trim()));
            intent.putExtra("from", Constant.PASSENGER_PUBLISH_PAY);
            intent.putExtra("id",serviceId);
            intent.putExtra("alipay",Common.PASSENGER_PUBLISH_ALIPAY);
            intent.putExtra("yivipay",Common.PASSENGER_PUBLISH_RESTPAY);
            intent.putExtra("type",Constant.RELEASE_OK);
            intent.putExtra("text_1",tv_start_time.getText().toString());
            intent.putExtra("text_2",data.getString("orderCode"));
            intent.putExtra("departTime",departTime);
            startActivity(intent);
        }else if (fromWhere.equals(Constant.HOST_PUBLISH_NOW_NEED_LOSS)){
                JSONObject data = obj.getJSONObject("data");
                Log.d("tag", "onSuccess: 你好---"+data.getString("orderId"));
                Intent intent = new Intent(OrderConfirmActivity.this, BookPayActivity.class);
                intent.putExtra("orderCode",data.getString("orderId"));
                intent.putExtra("amount",HandleDataUtil.withoutChinese(tv_break_appointment_money.getText().toString().trim()));
                intent.putExtra("from", Constant.HOST_PUBLISH_PAY);
                intent.putExtra("id",serviceId);
                intent.putExtra("alipay",Common.HOST_PUBLISH_ALIPAY);
                intent.putExtra("yivipay",Common.HOST_PUBLISH_RESTPAY);
                intent.putExtra("type",Constant.RELEASE_OK);
                intent.putExtra("text_1",tv_start_time.getText().toString());
                intent.putExtra("text_2",data.getString("orderCode"));
                intent.putExtra("departTime",departTime);
                startActivity(intent);

        }else if (fromWhere.equals(Constant.HOST_PUBLISH_NOW_NOT_NEED_LOSS)){
            showToast("免缴爽约金");
            JSONObject data = obj.getJSONObject("data");
            Intent intent = new Intent(this, CompleteActivity.class);
            intent.putExtra("type",Constant.RELEASE_OK);
            intent.putExtra("textk_2",data.getString("orderCode"));
            intent.putExtra("text_1",tv_start_time.getText().toString());
            startActivity(intent);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }
}
